<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/25">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Nginx/12. LNMP常见的502错误/">LNMP常见的502错误</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于 LNMP 来说，最常见的问题就是502问题了。配置完环境后，一访问网站直接提示“502 Bad Gateway”。出现502的原因大致分为两种</p>
<ol>
<li>配置错误</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为nginx找不到php-fpm了，所以报错，一般是fastcgi_pass后面的路径配置错误了，后面可以是socket或者是ip:port</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 nginx 中有配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">location ~ \.php$ &#123;</div><div class="line">      include fastcgi_params;</div><div class="line">      fastcgi_pass unix:/tmp/php-fcgi.sock;</div><div class="line">      fastcgi_index index.php;</div><div class="line">      fastcgi_param SCRIPT_FILENAME /usr/<span class="built_in">local</span>/nginx/html<span class="variable">$fastcgi_script_name</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果把 fastcgi_pass 后面指定路径配置错了，那么就会出现502错误，因为 nginx 找不到 php-fpm 了。fastcgi_pass 后面可以跟 socket 也可以跟 ip：port，默认监听地址为 127.0.0.1：9000.</p>
<ol>
<li>资源耗尽</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lnmp架构在处理php时，nginx直接调取后端的php-fpm服务，如果nginx的请求量偏高，我们又没有给php-fpm配置足够的子进程，那么php-fpm就会资源耗尽，一旦资源耗尽nginx找不到php-fpm就会出现502错误，</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决方案：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;去调整php-fpm.conf中的pm.max_children数值，使其增加，但是也不能无限增加，毕竟资源有限，一般4G内存机器如果跑php-fpm和nginx，不跑mysql可以设置为150，8G为300以此类推</p>
<ol>
<li>除了上面的两种错误还有其他的原因，很少有，我们可以借助nginx的错误日志来进行排查</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /usr/<span class="built_in">local</span>/nginx/logs/nginx_error.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们也可以给日志定义级别vim/usr/local/nginx/conf/nginx.conf 找到error_log，默认是crit最严谨的就行，也可以改成debug显示的信息最全面，但是很容易撑爆我们的磁盘。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先我们需要让浏览器进行访问</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改nginx的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># vim/usr/local/nginx/conf/vhosts/111.conf</span></div><div class="line"> </div><div class="line">server</div><div class="line">&#123;</div><div class="line"> listen 80;</div><div class="line"> server_name www.111.com; <span class="comment">#域名地址</span></div><div class="line"> index index.html index.htm index.php;</div><div class="line"> root /data/www/;</div><div class="line"> </div><div class="line"> location ~ \.php$ &#123;</div><div class="line"> include fastcgi_params;</div><div class="line"> fastcgi_pass unix:/tmp/www.sock; //修改sock</div><div class="line"> <span class="comment">#fastcgi_pass 127.0.0.1:9000;</span></div><div class="line"> fastcgi_index index.php;</div><div class="line"> fastcgi_param SCRIPT_FILENAME /data/www<span class="variable">$fastcgi_script_name</span>;</div><div class="line"> &#125;</div><div class="line"> </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查语法是否正常</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment">#/usr/local/nginx/sbin/nginx -t</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重新加载配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># /usr/local/nginx/sbin/nginx-s reload</span></div><div class="line">[root@wqslinux ~]<span class="comment"># /etc/init.d/nginx reload</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查nginx是那个用户跑的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># ps aux |grep nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑php-fpm文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们要在这个php-fpm文件里面设置nginx的用户主，跟组这样才不会显示502</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># vim/usr/local/php/etc/php-fpm.conf</span></div><div class="line"></div><div class="line"></div><div class="line">[global]</div><div class="line">pid = /usr/<span class="built_in">local</span>/php/var/run/php-fpm.pid</div><div class="line">error_log =/usr/<span class="built_in">local</span>/php/var/<span class="built_in">log</span>/php-fpm.log</div><div class="line">[www]</div><div class="line">listen = /tmp/www.sock</div><div class="line">user = php-fpm</div><div class="line">group = php-fpm</div><div class="line">listen.owner = nobody //定义属主</div><div class="line">listen.group = nobody //定义属组</div><div class="line">pm = dynamic</div><div class="line">pm.max_children = 50</div><div class="line">pm.start_servers = 20</div><div class="line">pm.min_spare_servers = 5</div><div class="line">pm.max_spare_servers = 35</div><div class="line">pm.max_requests = 500</div><div class="line">rlimit_files = 1024</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置完之后重启php-fpm</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># /etc/init.d/php-fpm restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ps： 再补充一个，是近期遇到的问题</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这种情况下，<strong>使用的是socket，版本高于5.4（含5.4） 默认监听的socket文件权限是所有者只读，属组和其他用户没有任何权限</strong>。所以，nginx的启动用户（咱们配置的是 nobody）就没有办法去读这个socket文件，最终导致502，这个问题可以在nginx的错误 日志中发现。解决办法很简单，上面给出的配置文件中就有避免这个问题的配置。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">listen.owner = nobody    <span class="comment">#定义属主</span></div><div class="line">listen.group = nobody    <span class="comment">#定义属组</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这两个配置就是定义socket的属主和属组是谁。除了这个还有一种方法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">listen.mode = 777</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样nobody也可以有读取权限了。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nginx/11. php-fpm配置文件/">php-fpm配置文件 高并发参数配置及linux内核参数优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;  之前给出了 php-fpm.conf 的内容，下面给出一个模版，配置如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[global]</div><div class="line">pid = /usr/<span class="built_in">local</span>/php/var/run/php-fpm.pid</div><div class="line">error_log = /usr/<span class="built_in">local</span>/php/var/<span class="built_in">log</span>/php-fpm.log</div><div class="line">[www]</div><div class="line">listen = /tmp/php-fcgi.sock</div><div class="line">user = php-fpm</div><div class="line">group = php-fpm</div><div class="line">listen.owner = nobody <span class="comment">#和后面的nginx一致</span></div><div class="line">listen.group = nobody <span class="comment">#同上</span></div><div class="line">pm = dynamic</div><div class="line">pm.max_children = 50</div><div class="line">pm.start_servers = 20</div><div class="line">pm.min_spare_servers = 5</div><div class="line">pm.max_spare_servers = 35</div><div class="line">pm.max_requests = 500</div><div class="line">rlimit_files = 1024</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[global]  部分是全局配置，指定 pid 文件路径以及 error_log 路径。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[www] 是一个 pool ，还可以再写第二个 pool ，第二个 pool 和第一个不易演的的地方，首先 pool 的 name ，比如叫做  [www2] 。然后 listen 肯定就不能一样了，比如 可以 listen = /tmp/php-fcgi2.sock 。而 user ， group 也可以和   [www] 中定义的不一样。 listen.owner 这个是定义 /tmp/php-fcgi2.sock 这个文件的所有者是谁，在 php5.4 版本之后监听的 socket 文件权限默认变成了 600，如果不定义 listen.owner 那么 nginx 调用这个 socket 的时候就没有权限了，所以在这里定义 listen.owner 为 nginx 的子进程监听用户。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pm = dynamic 表示以动态的形式启动，在 php5.3 版本以后它可以支持动态和静态了，如果是静态，即 pm = static 时，下面的配置只有 pm.max.children 管用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pm.max.children 表示启动几个 php-fpm 的子进程。如果是 dynamic ，下面的配置会生效，pm.max.children 表示最大可以启动几个子进程。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pm.start_servers 表示一开始启动几个子进程。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pm.max_requeste 表示一个子进程最多可以接受多少个请求，比如设置为500那么一个子进程手里500个请求后自动销毁。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rlimit_files 表示每个子进程打开的多少个文件句柄。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">slowlog = /tmp/www_slow.log</div><div class="line">request_slowlog_timeout = 1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;表示一个脚本执行时间超过 1秒就要记录这个slow.log ，记录这个可以看到这个脚本哪里执行慢，可以通过slow.log排查网站慢的原因，根据这个原因做一定的优化。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">php_admin_value [open_basedir] = /data/www/:/tmp</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;apache 可以设置 open_basedir ，php-fpm也可以，不同的 pool 设置不同的 open_basedir 可以针对不同的域名进行不同的限制。多个目录用 ：分割。 </p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nginx/10. nginx配置详解/">nginx配置详解</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div><div class="line">341</div><div class="line">342</div><div class="line">343</div><div class="line">344</div><div class="line">345</div><div class="line">346</div><div class="line">347</div><div class="line">348</div><div class="line">349</div><div class="line">350</div><div class="line">351</div><div class="line">352</div><div class="line">353</div><div class="line">354</div><div class="line">355</div><div class="line">356</div><div class="line">357</div><div class="line">358</div><div class="line">359</div><div class="line">360</div><div class="line">361</div><div class="line">362</div><div class="line">363</div><div class="line">364</div><div class="line">365</div><div class="line">366</div><div class="line">367</div><div class="line">368</div><div class="line">369</div><div class="line">370</div><div class="line">371</div><div class="line">372</div><div class="line">373</div><div class="line">374</div><div class="line">375</div><div class="line">376</div><div class="line">377</div><div class="line">378</div><div class="line">379</div><div class="line">380</div><div class="line">381</div><div class="line">382</div><div class="line">383</div><div class="line">384</div><div class="line">385</div><div class="line">386</div><div class="line">387</div><div class="line">388</div><div class="line">389</div><div class="line">390</div><div class="line">391</div><div class="line">392</div><div class="line">393</div><div class="line">394</div></pre></td><td class="code"><pre><div class="line">user nginx ;</div><div class="line"><span class="comment">#用户</span></div><div class="line"></div><div class="line">worker_processes 8;</div><div class="line"><span class="comment">#工作进程，根据硬件调整，大于等于cpu核数</span></div><div class="line"></div><div class="line">error_log logs/nginx_error.log crit;</div><div class="line"><span class="comment">#错误日志</span></div><div class="line"></div><div class="line">pid logs/nginx.pid;</div><div class="line"><span class="comment">#pid放置的位置</span></div><div class="line"></div><div class="line">worker_rlimit_nofile 204800;</div><div class="line"><span class="comment">#指定进程可以打开的最大描述符</span></div><div class="line">这个指令是指当一个nginx进程打开的最多文件描述符数目，理论值应该是最多打开文</div><div class="line">件数（<span class="built_in">ulimit</span> -n）与nginx进程数相除，但是nginx分配请求并不是那么均匀，所以最好与<span class="built_in">ulimit</span> -n 的值保持一致。</div><div class="line">现在在linux 2.6内核下开启文件打开数为65535，worker_rlimit_nofile就相应应该填写65535。</div><div class="line">这是因为nginx调度时分配请求到进程并不是那么的均衡，所以假如填写10240，总并发量达到3-4万时就有进程可能超过10240了，这时会返回502错误。</div><div class="line"></div><div class="line">events</div><div class="line"></div><div class="line">&#123;</div><div class="line">use epoll;</div><div class="line"><span class="comment">#使用epoll的I/O 模型</span></div><div class="line">补充说明:</div><div class="line">与apache相类，nginx针对不同的操作系统，有不同的事件模型</div><div class="line">A）标准事件模型</div><div class="line">Select、poll属于标准事件模型，如果当前系统不存在更有效的方法，nginx会选择select或poll</div><div class="line">B）高效事件模型</div><div class="line">Kqueue：使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。</div><div class="line">Epoll:使用于Linux内核2.6版本及以后的系统。</div><div class="line">/dev/poll：使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。</div><div class="line">Eventport：使用于Solaris 10. 为了防止出现内核崩溃的问题， 有必要安装安全补丁</div><div class="line"></div><div class="line"></div><div class="line">worker_connections 204800;</div><div class="line"><span class="comment">#工作进程的最大连接数量，根据硬件调整，和前面工作进程配合起来用，尽量大，但是别把cpu跑到100%就行</span></div><div class="line">每个进程允许的最多连接数， 理论上每台nginx服务器的最大连接数为worker_processes*worker_connections</div><div class="line"></div><div class="line">keepalive_timeout 60;</div><div class="line"></div><div class="line">keepalive超时时间。</div><div class="line"></div><div class="line">client_header_buffer_size 4k;</div><div class="line"></div><div class="line">客户端请求头部的缓冲区大小，这个可以根据你的系统分页大小来设置，一般一个请求头的大小不会超过1k，不过由于一般系统分页都要大于1k，所以这里设置为分页大小。</div><div class="line">分页大小可以用命令getconf PAGESIZE 取得。</div><div class="line">[root@web001 ~]<span class="comment"># getconf PAGESIZE</span></div><div class="line">4096</div><div class="line">但也有client_header_buffer_size超过4k的情况，但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。</div><div class="line"></div><div class="line">open_file_cache max=65535 inactive=60s;</div><div class="line"></div><div class="line">这个将为打开文件指定缓存，默认是没有启用的，max指定缓存数量，建议和打开文件数一致，inactive是指经过多长时间文件没被请求后删除缓存。</div><div class="line"></div><div class="line">open_file_cache_valid 80s;</div><div class="line"></div><div class="line">这个是指多长时间检查一次缓存的有效信息。</div><div class="line"></div><div class="line">open_file_cache_min_uses 1;</div><div class="line"></div><div class="line">open_file_cache指令中的inactive参数时间内文件的最少使用次数，如果超过这个数字，文件描述符一直是在缓存中打开的，如上例，如果有一个文件在inactive时间内一次没被使用，它将被移除。</div><div class="line"></div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">#设定http服务器，利用它的反向代理功能提供负载均衡支持</span></div><div class="line">http</div><div class="line">&#123;</div><div class="line">include mime.types;</div><div class="line"><span class="comment">#设定mime类型,类型由mime.type文件定义</span></div><div class="line">default_type application/octet-stream;</div><div class="line">log_format main <span class="string">'$host $status [$time_local] $remote_addr [$time_local] $request_uri '</span></div><div class="line"><span class="string">'"$http_referer" "$http_user_agent" "$http_x_forwarded_for" '</span></div><div class="line"><span class="string">'$bytes_sent $request_time $sent_http_x_cache_hit'</span>;</div><div class="line">log_format log404 <span class="string">'$status [$time_local] $remote_addr $host$request_uri $sent_http_location'</span>;</div><div class="line"><span class="variable">$remote_addr</span>与<span class="variable">$http_x_forwarded_for</span>用以记录客户端的ip地址；</div><div class="line"><span class="variable">$remote_user</span>：用来记录客户端用户名称；</div><div class="line"><span class="variable">$time_local</span>： 用来记录访问时间与时区；</div><div class="line"><span class="variable">$request</span>： 用来记录请求的url与http协议；</div><div class="line"><span class="variable">$status</span>： 用来记录请求状态；成功是200，</div><div class="line"><span class="variable">$body_bytes_s</span> ent ：记录发送给客户端文件主体内容大小；</div><div class="line"><span class="variable">$http_referer</span>：用来记录从那个页面链接访问过来的；</div><div class="line"><span class="variable">$http_user_agent</span>：记录客户毒啊浏览器的相关信息；</div><div class="line">通常web服务器放在反向代理的后面，这样就不能获取到客户的IP地址了，通过<span class="variable">$remote_add</span>拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中，可以增加x_forwarded_for信息，用以记录原有客户端的IP地址和原来客户端的请求的服务器地址；</div><div class="line">access_log /dev/null;</div><div class="line"><span class="comment">#用了log_format指令设置了日志格式之后，需要用access_log指令指定日志文件的存放路径；</span></div><div class="line"><span class="comment"># access_log /usr/local/nginx/logs/access_log main;</span></div><div class="line">server_names_hash_bucket_size 128;</div><div class="line"><span class="comment">#保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小，并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后，使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小，那么在查找键的时候，最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址，第二次是在存储单元中查找键 值。因此，如果Nginx给出需要增大hash max size 或 hash bucket size的提示，那么首要的是增大前一个参数的大小.</span></div><div class="line"></div><div class="line">client_header_buffer_size 4k;</div><div class="line">客户端请求头部的缓冲区大小，这个可以根据你的系统分页大小来设置，一般一个请求的头部大小不会超过1k，不过由于一般系统分页都要大于1k，所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。</div><div class="line"></div><div class="line">large_client_header_buffers 8 128k;</div><div class="line">客户请求头缓冲大小</div><div class="line">nginx默认会用client_header_buffer_size这个buffer来读取header值，如果</div><div class="line">header过大，它会使用large_client_header_buffers来读取</div><div class="line">如果设置过小HTTP头/Cookie过大 会报400 错误nginx 400 bad request</div><div class="line">求行如果超过buffer，就会报HTTP 414错误(URI Too Long)</div><div class="line">nginx接受最长的HTTP头部大小必须比其中一个buffer大，否则就会报400的</div><div class="line">HTTP错误(Bad Request)。</div><div class="line">open_file_cache max 102400</div><div class="line">使用字段:http, server, location 这个指令指定缓存是否启用,如果启用,将记录文件以下信息: ·打开的文件描述符,大小信息和修改时间. ·存在的目录信息. ·在搜索文件过程中的错误信息 --没有这个文件,无法正确读取,参考open_file_cache_errors指令选项:</div><div class="line">·max -指定缓存的最大数目,如果缓存溢出,最长使用过的文件(LRU)将被移除</div><div class="line">例: open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;</div><div class="line">open_file_cache_errors</div><div class="line">语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.</div><div class="line">open_file_cache_min_uses</div><div class="line">语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如 果使用更大的值,文件描述符在cache中总是打开状态.</div><div class="line">open_file_cache_valid</div><div class="line">语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.</div><div class="line"></div><div class="line">client_max_body_size 300m;</div><div class="line">设定通过nginx上传文件的大小</div><div class="line"></div><div class="line">sendfile on;</div><div class="line"><span class="comment">#sendfile指令指定 nginx 是否调用sendfile 函数（zero copy 方式）来输出文件，</span></div><div class="line">对于普通应用，必须设为on。</div><div class="line">如果用来进行下载等应用磁盘IO重负载应用，可设置为off，以平衡磁盘与网络IO处理速度，降低系统uptime。</div><div class="line">tcp_nopush on;</div><div class="line">此选项允许或禁止使用socke的TCP_CORK的选项，此选项仅在使用sendfile的时候使用</div><div class="line"></div><div class="line">proxy_connect_timeout 90; </div><div class="line"><span class="comment">#后端服务器连接的超时时间_发起握手等候响应超时时间</span></div><div class="line">proxy_read_timeout 180;</div><div class="line"><span class="comment">#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理（也可以说是后端服务器处理请求的时间）</span></div><div class="line">proxy_send_timeout 180;</div><div class="line"><span class="comment">#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据</span></div><div class="line">proxy_buffer_size 256k;</div><div class="line"><span class="comment">#设置从被代理服务器读取的第一部分应答的缓冲区大小，通常情况下这部分应答中包含一个小的应答头，默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小，不过可以将其设置为更小</span></div><div class="line">proxy_buffers 4 256k;</div><div class="line"><span class="comment">#设置用于读取应答（来自被代理服务器）的缓冲区数目和大小，默认情况也为分页大小，根据操作系统的不同可能是4k或者8k</span></div><div class="line">proxy_busy_buffers_size 256k;</div><div class="line"></div><div class="line">proxy_temp_file_write_size 256k;</div><div class="line"><span class="comment">#设置在写入proxy_temp_path时数据的大小，预防一个工作进程在传递文件时阻塞太长</span></div><div class="line">proxy_temp_path /data0/proxy_temp_dir;</div><div class="line"><span class="comment">#proxy_temp_path和proxy_cache_path指定的路径必须在同一分区</span></div><div class="line">proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;</div><div class="line"><span class="comment">#设置内存缓存空间大小为200MB，1天没有被访问的内容自动清除，硬盘缓存空间大小为30GB。</span></div><div class="line"></div><div class="line">keepalive_timeout 120;</div><div class="line">keepalive超时时间。</div><div class="line">tcp_nodelay on;</div><div class="line">client_body_buffer_size 512k;</div><div class="line">如果把它设置为比较大的数值，例如256k，那么，无论使用firefox还是IE浏览器，来提交任意小于256k的图片，都很正常。如果注释该指令，使用默认的client_body_buffer_size设置，也就是操作系统页面大小的两倍，8k或者16k，问题就出现了。</div><div class="line">无论使用firefox4.0还是IE8.0，提交一个比较大，200k左右的图片，都返回500 Internal Server Error错误</div><div class="line">proxy_intercept_errors on;</div><div class="line">表示使nginx阻止HTTP应答代码为400或者更高的应答。</div><div class="line"></div><div class="line">upstream img_relay &#123;</div><div class="line">server 127.0.0.1:8027;</div><div class="line">server 127.0.0.1:8028;</div><div class="line">server 127.0.0.1:8029;</div><div class="line"><span class="built_in">hash</span> <span class="variable">$request_uri</span>;</div><div class="line">&#125;</div><div class="line">nginx的upstream目前支持4种方式的分配</div><div class="line">1、轮询（默认）</div><div class="line">每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器down掉，能自动剔除。</div><div class="line">2、weight</div><div class="line">指定轮询几率，weight和访问比率成正比，用于后端服务器性能不均的情况。</div><div class="line">例如：</div><div class="line">upstream bakend &#123;</div><div class="line">server 192.168.0.14 weight=10;</div><div class="line">server 192.168.0.15 weight=10;</div><div class="line">&#125;</div><div class="line">2、ip_hash</div><div class="line">每个请求按访问ip的<span class="built_in">hash</span>结果分配，这样每个访客固定访问一个后端服务器，可以解决session的问题。</div><div class="line">例如：</div><div class="line">upstream bakend &#123;</div><div class="line">ip_hash;</div><div class="line">server 192.168.0.14:88;</div><div class="line">server 192.168.0.15:80;</div><div class="line">&#125;</div><div class="line">3、fair（第三方）</div><div class="line">按后端服务器的响应时间来分配请求，响应时间短的优先分配。</div><div class="line">upstream backend &#123;</div><div class="line">server server1;</div><div class="line">server server2;</div><div class="line">fair;</div><div class="line">&#125;</div><div class="line">4、url_hash（第三方）</div><div class="line">按访问url的<span class="built_in">hash</span>结果来分配请求，使每个url定向到同一个后端服务器，后端服务器为缓存时比较有效。</div><div class="line">例：在upstream中加入<span class="built_in">hash</span>语句，server语句中不能写入weight等其他的参数，hash_method是使用的<span class="built_in">hash</span>算法</div><div class="line">upstream backend &#123;</div><div class="line">server squid1:3128;</div><div class="line">server squid2:3128;</div><div class="line"><span class="built_in">hash</span> <span class="variable">$request_uri</span>;</div><div class="line">hash_method crc32;</div><div class="line">&#125;</div><div class="line">tips:</div><div class="line">upstream bakend&#123;<span class="comment">#定义负载均衡设备的Ip及设备状态</span></div><div class="line">ip_hash;</div><div class="line">server 127.0.0.1:9090 down;</div><div class="line">server 127.0.0.1:8080 weight=2;</div><div class="line">server 127.0.0.1:6060;</div><div class="line">server 127.0.0.1:7070 backup;</div><div class="line">&#125;</div><div class="line">在需要使用负载均衡的server中增加</div><div class="line">proxy_pass http://bakend/;</div><div class="line">每个设备的状态设置为:</div><div class="line">1.down表示单前的server暂时不参与负载</div><div class="line">2.weight默认为1.weight越大，负载的权重就越大。</div><div class="line">3.max_fails：允许请求失败的次数默认为1.当超过最大次数时，返回proxy_next_upstream模块定义的错误</div><div class="line">4.fail_timeout:max_fails次失败后，暂停的时间。</div><div class="line">5.backup： 其它所有的非backup机器down或者忙的时候，请求backup机器。所以这台机器压力会最轻。</div><div class="line">nginx支持同时设置多组的负载均衡，用来给不用的server来使用。</div><div class="line">client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug</div><div class="line">client_body_temp_path设置记录文件的目录 可以设置最多3层目录</div><div class="line">location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡</div><div class="line"></div><div class="line">server</div><div class="line"><span class="comment">#配置虚拟机</span></div><div class="line">&#123;</div><div class="line">listen 80;</div><div class="line"><span class="comment">#配置监听端口</span></div><div class="line">server_name image.***.com;</div><div class="line"><span class="comment">#配置访问域名</span></div><div class="line">location ~* \.(mp3|exe)$ &#123;</div><div class="line"><span class="comment">#对以“mp3或exe”结尾的地址进行负载均衡</span></div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line"><span class="comment">#设置被代理服务器的端口或套接字，以及URL</span></div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line"><span class="comment">#以上三行，目的是将代理服务器收到的用户的信息传到真实服务器上</span></div><div class="line">&#125;</div><div class="line">location /face &#123;</div><div class="line"><span class="keyword">if</span> (<span class="variable">$http_user_agent</span> ~* <span class="string">"xnp"</span>) &#123;</div><div class="line">rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;</div><div class="line">&#125;</div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line">error_page 404 502 = @fetch;</div><div class="line">&#125;</div><div class="line">location @fetch &#123;</div><div class="line">access_log /data/logs/face.log log404;</div><div class="line"><span class="comment">#设定本服务器的访问日志</span></div><div class="line">rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;</div><div class="line">&#125;</div><div class="line"></div><div class="line">location /image &#123;</div><div class="line"><span class="keyword">if</span> (<span class="variable">$http_user_agent</span> ~* <span class="string">"xnp"</span>) &#123;</div><div class="line">rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;</div><div class="line">&#125;</div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line">error_page 404 502 = @fetch;</div><div class="line">&#125;</div><div class="line">location @fetch &#123;</div><div class="line">access_log /data/logs/image.log log404;</div><div class="line">rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">server</div><div class="line">&#123;</div><div class="line">listen 80;</div><div class="line">server_name *.***.com *.***.cn;</div><div class="line">location ~* \.(mp3|exe)$ &#123;</div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line">&#125;</div><div class="line">location / &#123;</div><div class="line"><span class="keyword">if</span> (<span class="variable">$http_user_agent</span> ~* <span class="string">"xnp"</span>) &#123;</div><div class="line">rewrite ^(.*)$ http://i1.***img.com/<span class="built_in">help</span>/noimg.gif redirect;</div><div class="line">&#125;</div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line"><span class="comment">#error_page 404 http://i1.***img.com/help/noimg.gif;</span></div><div class="line">error_page 404 502 = @fetch;</div><div class="line">&#125;</div><div class="line">location @fetch &#123;</div><div class="line">access_log /data/logs/baijiaqi.log log404;</div><div class="line">rewrite ^(.*)$ http://i1.***img.com/<span class="built_in">help</span>/noimg.gif redirect;</div><div class="line">&#125;</div><div class="line"><span class="comment">#access_log off;</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">server</div><div class="line">&#123;</div><div class="line">listen 80;</div><div class="line">server_name *.***img.com;</div><div class="line"></div><div class="line">location ~* \.(mp3|exe)$ &#123;</div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line">location / &#123;</div><div class="line"><span class="keyword">if</span> (<span class="variable">$http_user_agent</span> ~* <span class="string">"xnp"</span>) &#123;</div><div class="line">rewrite ^(.*)$ http://i1.***img.com/<span class="built_in">help</span>/noimg.gif;</div><div class="line">&#125;</div><div class="line">proxy_pass http://img_relay<span class="variable">$request_uri</span>;</div><div class="line">proxy_set_header Host <span class="variable">$host</span>;</div><div class="line">proxy_set_header X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">proxy_set_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</div><div class="line"><span class="comment">#error_page 404 http://i1.***img.com/help/noimg.gif;</span></div><div class="line">error_page 404 = @fetch;</div><div class="line">&#125;</div><div class="line"><span class="comment">#access_log off;</span></div><div class="line">location @fetch &#123;</div><div class="line">access_log /data/logs/baijiaqi.log log404;</div><div class="line">rewrite ^(.*)$ http://i1.***img.com/<span class="built_in">help</span>/noimg.gif redirect;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">server</div><div class="line">&#123;</div><div class="line">listen 8080;</div><div class="line">server_name ngx-ha.***img.com;</div><div class="line">location / &#123;</div><div class="line">stub_status on;</div><div class="line">access_log off;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">server &#123;</div><div class="line">listen 80;</div><div class="line">server_name imgsrc1.***.net;</div><div class="line">root html;</div><div class="line">&#125;</div><div class="line">server &#123;</div><div class="line">listen 80;</div><div class="line">server_name ***.com w.***.com;</div><div class="line"><span class="comment"># access_log /usr/local/nginx/logs/access_log main;</span></div><div class="line">location / &#123;</div><div class="line">rewrite ^(.*)$ http://www.***.com/ ;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">server &#123;</div><div class="line">listen 80;</div><div class="line">server_name *******.com w.*******.com;</div><div class="line"><span class="comment"># access_log /usr/local/nginx/logs/access_log main;</span></div><div class="line">location / &#123;</div><div class="line">rewrite ^(.*)$ http://www.*******.com/;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">server &#123;</div><div class="line">listen 80;</div><div class="line">server_name ******.com;</div><div class="line"><span class="comment"># access_log /usr/local/nginx/logs/access_log main;</span></div><div class="line">location / &#123;</div><div class="line">rewrite ^(.*)$ http://www.******.com/;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">location /NginxStatus &#123;</div><div class="line">stub_status on;</div><div class="line">access_log on;</div><div class="line">auth_basic <span class="string">"NginxStatus"</span>;</div><div class="line">auth_basic_user_file conf/htpasswd;</div><div class="line">&#125;</div><div class="line"><span class="comment">#设定查看Nginx状态的地址</span></div><div class="line"></div><div class="line">location ~ /\.ht &#123;</div><div class="line">deny all;</div><div class="line">&#125;</div><div class="line"><span class="comment">#禁止访问.htxxx文件</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">注释：变量</div><div class="line">Ngx_http_core_module模块支持内置变量，他们的名字和apache的内置变量是一致的。</div><div class="line">首先是说明客户请求title中的行，例如<span class="variable">$http_user_agent</span>,<span class="variable">$http_cookie</span>等等。</div><div class="line">此外还有其它的一些变量</div><div class="line"><span class="variable">$args</span>此变量与请求行中的参数相等</div><div class="line"><span class="variable">$content_length</span>等于请求行的“Content_Length”的值。</div><div class="line"><span class="variable">$content_type</span>等同与请求头部的”Content_Type”的值</div><div class="line"><span class="variable">$document_root</span>等同于当前请求的root指令指定的值</div><div class="line"><span class="variable">$document_uri</span>与<span class="variable">$uri</span>一样</div><div class="line"><span class="variable">$host</span>与请求头部中“Host”行指定的值或是request到达的server的名字（没有Host行）一样</div><div class="line"><span class="variable">$limit_rate</span>允许限制的连接速率</div><div class="line"><span class="variable">$request_method</span>等同于request的method，通常是“GET”或“POST”</div><div class="line"><span class="variable">$remote_addr</span>客户端ip</div><div class="line"><span class="variable">$remote_port</span>客户端port</div><div class="line"><span class="variable">$remote_user</span>等同于用户名，由ngx_http_auth_basic_module认证</div><div class="line"><span class="variable">$request_filename</span>当前请求的文件的路径名，由root或<span class="built_in">alias</span>和URI request组合而成</div><div class="line"><span class="variable">$request_body_file</span></div><div class="line"><span class="variable">$request_uri</span>含有参数的完整的初始URI</div><div class="line"><span class="variable">$query_string</span>与<span class="variable">$args</span>一样</div><div class="line"><span class="variable">$sheeme</span> http模式（http,https）尽在要求是评估例如</div><div class="line">Rewrite ^(.+)$ <span class="variable">$sheme</span>://example.com$; Redirect;</div><div class="line"><span class="variable">$server_protocol</span>等同于request的协议，使用“HTTP/或“HTTP/</div><div class="line"><span class="variable">$server_addr</span> request到达的server的ip，一般获得此变量的值的目的是进行系统调用。为了避免系统调用，有必要在listen指令中指明ip，并使用<span class="built_in">bind</span>参数。</div><div class="line"><span class="variable">$server_name</span>请求到达的服务器名</div><div class="line"><span class="variable">$server_port</span>请求到达的服务器的端口号</div><div class="line"><span class="variable">$uri</span>等同于当前request中的URI，可不同于初始值，例如内部重定向时或使用index</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nginx/1. Nginx编译安装/">Nginx编译安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://nginx.org" target="_blank" rel="external">Nginx官方网站</a>, 从官方网站可以看到Nginx更新速度很快，这也反映了一个事实，目前使用Nginx跑网站的公司或者个人越来越多。</p>
<h2 id="1-下载-Nginx"><a href="#1-下载-Nginx" class="headerlink" title="1.下载 Nginx"></a>1.下载 Nginx</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/src/</div><div class="line">wget http://nginx.org/download/nginx-1.4.4.tar.gz</div></pre></td></tr></table></figure>
<h2 id="2-解压-nginx"><a href="#2-解压-nginx" class="headerlink" title="2.解压 nginx"></a>2.解压 nginx</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar zxvf nginx-1.4.4.tar.gz</div></pre></td></tr></table></figure>
<h2 id="3-配置编译参数"><a href="#3-配置编译参数" class="headerlink" title="3.配置编译参数"></a>3.配置编译参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> nginx-1.4.4</div><div class="line">./configure \</div><div class="line">--prefix=/usr/<span class="built_in">local</span>/nginx \</div><div class="line">--with-http_realip_module \</div><div class="line">--with-http_sub_module \</div><div class="line">--with-http_gzip_static_module \</div><div class="line">--with-http_stub_status_module \</div><div class="line">--with-pcre</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译报错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">./configure: error: the HTTP rewrite module requires the PCRE library.</div><div class="line">```bash</div><div class="line"></div><div class="line">&amp;<span class="comment">#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;解决办法</span></div><div class="line"></div><div class="line">```bash</div><div class="line">yum -y install pcre-devel</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译报错</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nginx%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum -y install gcc</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译报错</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nginx%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y zlib-devel</div></pre></td></tr></table></figure>
<h2 id="4-编译、安装-nginx"><a href="#4-编译、安装-nginx" class="headerlink" title="4.编译、安装 nginx"></a>4.编译、安装 nginx</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为 nginx 比较小，所以很快就会安装完，而且也不会出什么错误。</p>
<h2 id="5-编写-nginx-启动脚本，并加入系统服务"><a href="#5-编写-nginx-启动脚本，并加入系统服务" class="headerlink" title="5.编写 nginx 启动脚本，并加入系统服务"></a>5.编写 nginx 启动脚本，并加入系统服务</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/init.d/nginx</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment"># chkconfig: - 30 21</span></div><div class="line"><span class="comment"># description: http service.</span></div><div class="line"><span class="comment"># Source Function Library</span></div><div class="line">. /etc/init.d/<span class="built_in">functions</span></div><div class="line"><span class="comment"># Nginx Settings</span></div><div class="line"></div><div class="line">NGINX_SBIN=<span class="string">"/usr/local/nginx/sbin/nginx"</span></div><div class="line">NGINX_CONF=<span class="string">"/usr/local/nginx/conf/nginx.conf"</span></div><div class="line">NGINX_PID=<span class="string">"/usr/local/nginx/logs/nginx.pid"</span></div><div class="line">RETVAL=0</div><div class="line">prog=<span class="string">"Nginx"</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">start</span></span>() &#123;</div><div class="line">        <span class="built_in">echo</span> -n $<span class="string">"Starting <span class="variable">$prog</span>: "</span></div><div class="line">        mkdir -p /dev/shm/nginx_temp</div><div class="line">        daemon <span class="variable">$NGINX_SBIN</span> -c <span class="variable">$NGINX_CONF</span></div><div class="line">        RETVAL=$?</div><div class="line">        <span class="built_in">echo</span></div><div class="line">        <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">stop</span></span>() &#123;</div><div class="line">        <span class="built_in">echo</span> -n $<span class="string">"Stopping <span class="variable">$prog</span>: "</span></div><div class="line">        killproc -p <span class="variable">$NGINX_PID</span> <span class="variable">$NGINX_SBIN</span> -TERM</div><div class="line">        rm -rf /dev/shm/nginx_temp</div><div class="line">        RETVAL=$?</div><div class="line">        <span class="built_in">echo</span></div><div class="line">        <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">reload</span></span>()&#123;</div><div class="line">        <span class="built_in">echo</span> -n $<span class="string">"Reloading <span class="variable">$prog</span>: "</span></div><div class="line">        killproc -p <span class="variable">$NGINX_PID</span> <span class="variable">$NGINX_SBIN</span> -HUP</div><div class="line">        RETVAL=$?</div><div class="line">        <span class="built_in">echo</span></div><div class="line">        <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">restart</span></span>()&#123;</div><div class="line">        stop</div><div class="line">        start</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">configtest</span></span>()&#123;</div><div class="line">    <span class="variable">$NGINX_SBIN</span> -c <span class="variable">$NGINX_CONF</span> -t</div><div class="line">    <span class="built_in">return</span> 0</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">  start)</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">  stop)</div><div class="line">        stop</div><div class="line">        ;;</div><div class="line">  reload)</div><div class="line">        reload</div><div class="line">        ;;</div><div class="line">  restart)</div><div class="line">        restart</div><div class="line">        ;;</div><div class="line">  configtest)</div><div class="line">        configtest</div><div class="line">        ;;</div><div class="line">  *)</div><div class="line">        <span class="built_in">echo</span> $<span class="string">"Usage: <span class="variable">$0</span> &#123;start|stop|reload|restart|configtest&#125;"</span></div><div class="line">        RETVAL=1</div><div class="line"><span class="keyword">esac</span></div><div class="line"></div><div class="line"><span class="built_in">exit</span> <span class="variable">$RETVAL</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，更改权限：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chmod 755 /etc/init.d/nginx</div><div class="line">chkconfig --add nginx</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果需要开机启动，执行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chkconfig nginx on</div></pre></td></tr></table></figure>
<h2 id="6-更改-nginx-配置"><a href="#6-更改-nginx-配置" class="headerlink" title="6.更改 nginx 配置"></a>6.更改 nginx 配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先把原来的配置文件清空</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt; /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;“&gt;”这个符号为重定向的意思，单独用它，可以把一个文本文档快速清空。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加如下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div></pre></td><td class="code"><pre><div class="line">user nobody nobody;</div><div class="line">worker_processes 2;</div><div class="line">error_log /usr/<span class="built_in">local</span>/nginx/logs/nginx_error.log crit;</div><div class="line">pid /usr/<span class="built_in">local</span>/nginx/logs/nginx.pid;</div><div class="line">worker_rlimit_nofile 51200;</div><div class="line"></div><div class="line">events</div><div class="line">&#123;</div><div class="line">    use epoll;</div><div class="line">    worker_connections 6000;</div><div class="line">&#125;</div><div class="line"></div><div class="line">http</div><div class="line">&#123;</div><div class="line">    include mime.types;</div><div class="line">    default_type application/octet-stream;</div><div class="line">    server_names_hash_bucket_size 3526;</div><div class="line">    server_names_hash_max_size 4096;</div><div class="line">    log_format combined_realip <span class="string">'$remote_addr $http_x_forwarded_for [$time_local]'</span></div><div class="line">    <span class="string">'$host "$request_uri" $status'</span></div><div class="line">    <span class="string">'"$http_referer" "$http_user_agent"'</span>;</div><div class="line">    sendfile on;</div><div class="line">    tcp_nopush on;</div><div class="line">    keepalive_timeout 30;</div><div class="line">    client_header_timeout 3m;</div><div class="line">    client_body_timeout 3m;</div><div class="line">    send_timeout 3m;</div><div class="line">    connection_pool_size 256;</div><div class="line">    client_header_buffer_size 1k;</div><div class="line">    large_client_header_buffers 8 4k;</div><div class="line">    request_pool_size 4k;</div><div class="line">    output_buffers 4 32k;</div><div class="line">    postpone_output 1460;</div><div class="line">    client_max_body_size 10m;</div><div class="line">    client_body_buffer_size 256k;</div><div class="line">    client_body_temp_path /usr/<span class="built_in">local</span>/nginx/client_body_temp;</div><div class="line">    proxy_temp_path /usr/<span class="built_in">local</span>/nginx/proxy_temp;</div><div class="line">    fastcgi_temp_path /usr/<span class="built_in">local</span>/nginx/fastcgi_temp;</div><div class="line">    fastcgi_intercept_errors on;</div><div class="line">    tcp_nodelay on;</div><div class="line">    gzip on;</div><div class="line">    gzip_min_length 1k;</div><div class="line">    gzip_buffers 4 8k;</div><div class="line">    gzip_comp_level 5;</div><div class="line">    gzip_http_version 1.1;</div><div class="line">    gzip_types text/plain application/x-javascript text/css text/htm application/xml;</div><div class="line"></div><div class="line">server</div><div class="line">&#123;</div><div class="line">    listen 80;</div><div class="line">    server_name localhost;</div><div class="line">    index index.html index.htm index.php;</div><div class="line">    root /usr/<span class="built_in">local</span>/nginx/html;</div><div class="line"></div><div class="line">    location ~ \.php$ &#123;</div><div class="line">        include fastcgi_params;</div><div class="line">        fastcgi_pass unix:/tmp/php-fcgi.sock;</div><div class="line">        fastcgi_index index.php;</div><div class="line">        fastcgi_param SCRIPT_FILENAME /usr/<span class="built_in">local</span>/nginx/html<span class="variable">$fastcgi_script_name</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存配置后，先检验一下配置文件是否有错误存在：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/nginx/sbin/nginx -t</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果显示内容如下，则配置正确，否则需要根据错误提示修改配置文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">nginx: the configuration file /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf syntax is ok</div><div class="line">nginx: configuration file /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf <span class="built_in">test</span> is successful</div><div class="line">```    </div><div class="line"></div><div class="line"><span class="comment">## 7.启动 nginx</span></div><div class="line"></div><div class="line">```bash</div><div class="line">service nginx start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不能启动，查看 /usr/local/nginx/logs/error.log 文件，检查 nginx 是否启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ps aux |grep nginx</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有进程</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/NFS/1. nfs部署和优化/">nfs部署和优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/NFS/">NFS</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;NFS = network file system</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;NFS服务比较常用，用于在网络上共享储存。</p>
<h2 id="服务端配置-NFS"><a href="#服务端配置-NFS" class="headerlink" title="服务端配置 NFS"></a>服务端配置 NFS</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;centos 上使用 NFS 服务，需要安装两个包（nfs-utils 和 rpcbind），不过当使用 yum 安装 nfs-utils 时会把 rpcbind 一起安装上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># yum install -y nfs-utils rpcbind</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以前的 centos 版本中，是需要安装 portmap 包的，从 centos 6 开始，就改为rpcbind了。NFS 配置起来很简单，只需要编辑配置文件 /etc/exports 即可。先创建一个简单的 NFS 服务器。 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /etc/exports</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入一下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/home/ 192.168.0.0/24(rw,sync,all_squash,anonuid=501,anongid=501)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个配置文件就这样简单一行。共分为三部分，第一部分就是本地要共享出去的目录，第二部分为允许访问的主机（可以是一个 IP 也可以是一个 IP 段），第三部分就是小括号里面的，为一些权限选项。</p>
<ul>
<li><p>rw： 读写；</p>
</li>
<li><p>ro： 只读；</p>
</li>
<li><p>sync： 同步模式，内存中数据时时写入磁盘；</p>
</li>
<li><p>async： 不同步，把内存中数据定期写入磁盘中；</p>
</li>
<li><p>no_root_squash： 加上这个选项后，root 用户就会对共享的目录拥有至高的权限控制，就像是对本机的目录操作一样。但这样不安全，不建议使用。</p>
</li>
<li><p>roo_squash： 和上面的选项对应，root 用户对共享目录权限不高，只有普通用户的权限，即限制了 root 。</p>
</li>
<li><p>all_squash： 不管使用 NFS 的用户是谁，他的身份都会被限定成一个指定的普通用户身份。</p>
</li>
<li><p>anonuid/anongid： 要和root_squash 以及 all_squash一同使用，用于指定使用 NFS 的用户限定后的 uid 和 gid ，前提是本机的 /etc/passwd 存在这个 uid 和 gid 。</p>
</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在分析一下刚才配置的那个 /etc/exports 文件。其中共享的目录为 /home ，信任的主机为 192.168.0.0/24 这个网段，权限为读写，同步，限定所有使用者，并且限定 uid 和 gid 都为501.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑好配置文件后，就该启动服务了 ：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># service rpcbind start</span></div><div class="line">正在启动 rpcbind： [确定]</div><div class="line">[root@localhost ~]<span class="comment"># service nfs start</span></div><div class="line">启动 NFS 服务： [确定]</div><div class="line">启动 NFS mountd： [确定]</div><div class="line">启动 NFS 守护进程： [确定]</div><div class="line">正在启动 RPC idmapd： [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在启动服务以前，需要先启动 rpcbind 服务，以前的 centos 老版本中并不是 rpcbind ，而是叫做 portmap。</p>
<h2 id="客户端上挂载-NFS"><a href="#客户端上挂载-NFS" class="headerlink" title="客户端上挂载 NFS"></a>客户端上挂载 NFS</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端挂载 NFS 以前，需要先查看服务端都共享了哪些目录，需要使用 showmount 命令，但这个命令是 nfs-utils 这个包带的，所以同样需要安装 nfs-utils</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y nfs-utils</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在可以查看服务器端都共享了那些目录了</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># showmount -e 192.168.0.73</span></div><div class="line">Export list <span class="keyword">for</span> 192.168.0.73:</div><div class="line">/home 192.168.0.0/24</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：其中 192.168.0.73 为 NFS 服务端 IP。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看到在服务器端配置的 nfs 共享信息。showmount -e 加 ip 就可以查看 nfs 的共享情况，可以看到 192.168.0.73 的共享目录为 /home ，信任主机为 192.168.0.0/24 这个网段。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面的命令是在客户端上挂载 nfs ：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mount -t nfs -o nfsvers=3 192.168.0.73:/home/ /mnt/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： -o 后面跟挂载选项，如果不加 -o nfsvers=3 则在挂载目录下的文件属主和组都是 nobody，如果指定 nfsers=3 则显示501，所以尽量加上这个选项，避免权限混乱。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nfs%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用 df -h 命令可以查看到多出来的一个 /mnt 分区，它就是 NFS 共享的目录了。</p>
<h2 id="命令-exportfs"><a href="#命令-exportfs" class="headerlink" title="命令 exportfs"></a>命令 exportfs</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一个常用的命令就是 exportfs ，它的常用选项为 [-aruv]</p>
<ul>
<li>-a： 全部挂载或者卸载；</li>
<li>-r： 重新挂载；</li>
<li>-u： 卸载某一个目录；</li>
<li>-v： 显示共享的目录</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 exportfs 命令，当改变 /etc/exports 配置文件后，不用重启 nfs 服务直接用这个 exportfs 即可。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面先更改服务端的配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># vim /etc/exports</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加一行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/tmp/ 192.168.0.0/24(rw,sync,no_root_squash)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后服务端上执行命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># exportfs -arv</span></div><div class="line">exporting 192.168.0.0/24:/tmp</div><div class="line">exporting 192.168.0.0/24:/home</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在之前的命令中用到了 mount 命令来挂载 nfs ，其实 mount 这个 nfs 服务还是有些说法的。首先是用 -t nfs 来指定挂载的类型为 nfs 另外在使用 nfs 时，常用一个选项就是 -o nolock 了，即在挂载 nfs 服务时，不加锁。在客户端上执行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir /test</span></div><div class="line">[root@192 ~]<span class="comment"># mount -t nfs -o nolock 192.168.0.73:/tmp/ /test/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以把要挂载的 nfs 目录写到 client 上的 /etc/fstab 文件中，挂载时只需要执行 mount -a 即可。在 /etc/fstab 里加一行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># vim /etc/fstab</span></div><div class="line"></div><div class="line">1192.168.0.73:/tmp/     /<span class="built_in">test</span>     nfs     nolock     0 0</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nfs%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为刚挂载过，所以先卸载：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># umount /test/</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nfs%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后执行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mount -a</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nfs%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样也可以挂载上，而且以后开机会自动挂载。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nagios/5. nagios调用短信接口/">nagios调用短信接口</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;公司正好有自己的短信接口平台，接下来带大家一起配置nagios调用第三方短信接口。</p>
<ol>
<li>首先我们要写一个调用短信接口的脚本，网上的脚本大都是python写的，我这个是shell写的，比较好理解。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /root/duanxin.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 脚本的日志文件 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">LOGFILE=<span class="string">"/data1/sms_log/sms_send_.log"</span>  <span class="comment">#定义发送短信的日志信息 文件</span></div><div class="line">:&gt;&gt;<span class="string">"<span class="variable">$LOGFILE</span>"</span> </div><div class="line"><span class="built_in">exec</span> 1&gt;&gt;<span class="string">"<span class="variable">$LOGFILE</span>"</span> </div><div class="line"><span class="built_in">exec</span> 2&gt;&amp;1 </div><div class="line"></div><div class="line">Uid=<span class="string">"test"</span>    <span class="comment">#接口的用户名，这个使用接口时对方会提供，我这里的test是随意写的</span></div><div class="line">Key=<span class="string">"123456"</span>  <span class="comment">#密码与用户名对应，也是接口方提供</span></div><div class="line"></div><div class="line">MOBILE_NUMBER=<span class="variable">$1</span> <span class="comment"># 接受短信的手机号码  </span></div><div class="line">QIANMING=<span class="string">"%e3%80%90%e9%a9%ac%e5%8f%af%e6%b3%a2%e7%bd%97%e7%bd%91%e3%80%91"</span> <span class="comment">#这里重点说一下，签名有的接口需要，有的不需要，因为我们公司的接口需要，所以需要添加上，我这里的签名内容是经过编码的，不加编码会导致发送失败，具体工作中需不需要编码还得看接口哪边有没有要求。</span></div><div class="line">XXD=<span class="string">"/usr/bin/xxd"</span> </div><div class="line">CURL=<span class="string">"/usr/bin/curl"</span> </div><div class="line">TIMEOUT=5 </div><div class="line">MESSAGE_ENCODE=$(<span class="built_in">echo</span> $(/usr/<span class="built_in">local</span>/bin/php -r <span class="string">"echo urlencode(\"<span class="variable">$2</span>\");"</span>; ) )  <span class="comment">#这里的$2是nagios发送短信的第二个变量</span></div><div class="line">URL=<span class="string">"http://192.168.100.100:8888/services/msgsend.asmx/SendMsg?userCode=<span class="variable">$&#123;$Uid&#125;</span>&amp;userPass=<span class="variable">$&#123;Key&#125;</span>&amp;DesNo=<span class="variable">$&#123;MOBILE_NUMBER&#125;</span>&amp;Msg=<span class="variable">$&#123;MESSAGE_ENCODE&#125;</span><span class="variable">$&#123;QIANMING&#125;</span>&amp;Channel=0"</span></div><div class="line"><span class="comment">#这里的URL是胡乱写的，不可能暴漏自己公司的接口哈，但是格式大体是这样的，到时候接口方会提供URL的格式的</span></div><div class="line"><span class="comment"># Send it </span></div><div class="line"><span class="built_in">set</span> -x </div><div class="line"><span class="variable">$&#123;CURL&#125;</span> -s --connect-timeout <span class="variable">$&#123;TIMEOUT&#125;</span> <span class="string">"<span class="variable">$&#123;URL&#125;</span>"</span></div></pre></td></tr></table></figure>
<ol>
<li>测试脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">bash /root/duanxin.sh <span class="string">"手机号"</span> “内容”</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果脚本报错，可以根据报错信息检查脚本，如果脚本没有问题，但是短信发不出去，可以看看sms_send_log里面的报错信息</p>
<ol>
<li>nagios 调用脚本，不要忘记脚本要给执行权限，一般脚本放在root目录下，nagios在调用脚本时是不能访问root目录的，所以你还要看你/root目录的权限</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">define <span class="built_in">command</span> &#123;</div><div class="line">       command_name host-notify-by-sms</div><div class="line">       command_line /root/duanxin.sh <span class="variable">$CONTACTPAGER</span>$ <span class="string">"<span class="variable">$HOSTNAME</span>$ <span class="variable">$HOSTSTATE</span>$ <span class="variable">$SHORTDATETIME</span>$"</span></div><div class="line">       &#125;</div><div class="line"></div><div class="line">define <span class="built_in">command</span> &#123;</div><div class="line">       command_name service-notify-by-sms</div><div class="line">       command_line /root/duanxin.sh <span class="variable">$CONTACTPAGER</span>$ <span class="string">"<span class="variable">$SERVICESTATE</span>$ <span class="variable">$SERVICEOUTPUT</span>$ <span class="variable">$HOSTALIAS</span>$/<span class="variable">$SERVICEDESC</span>$ <span class="variable">$SHORTDATETIME</span>$"</span></div><div class="line">       &#125;</div></pre></td></tr></table></figure>
<ol>
<li>看到这里大家可能对上面脚本的$1和$2概念比较模糊，其实刚开始我也迷糊，nagios怎么知道我要发送的号码呢，后来研究发现，$CONTACTPAGER$这个<br>量就是nagios内部联系人的变量，也就是他会调用我们在contacts.cfg里面定义的手机号，而我们脚本里面定义的$1就对应$CONTACTPAGER$，$2就对应”$HOSTNAME$ $HOSTSTATE$ $SHORTDATETIME$” </li>
</ol>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nagios/4. Nagios整合微信订阅号报警/">Nagios整合微信订阅号报警</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;环境：rhel6.5 selinux 和 iptables 关闭。要求能上外网的（虚拟机亲测可用）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下是nagios服务与微信订阅号的整合过程，最终实现当服务或主机出现故障，自动调用微信报警。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重点讲述如何配置微信公众平台私有接口，至于nagios服务的配置请大家参照nagios官方文档进行，此处不再赘述。（www.nagios.org）</p>
<h2 id="1-下载微信公众平台私有接口"><a href="#1-下载微信公众平台私有接口" class="headerlink" title="1.下载微信公众平台私有接口"></a>1.下载微信公众平台私有接口</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">yum install -y git</div><div class="line">git <span class="built_in">clone</span> https://github.com/lealife/WeiXin-Private-API</div></pre></td></tr></table></figure>
<h2 id="2-修改微信公众平台私有接口代码，以配合nagios报警"><a href="#2-修改微信公众平台私有接口代码，以配合nagios报警" class="headerlink" title="2.修改微信公众平台私有接口代码，以配合nagios报警"></a>2.修改微信公众平台私有接口代码，以配合nagios报警</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">cp -r WeiXin-Private-API /usr/<span class="built_in">local</span>/nagios/libexec/weixin</div><div class="line">chown -R nagios.nagios /usr/<span class="built_in">local</span>/nagios/libexec/weixin</div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nagios/libexec/weixin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改config.php文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$G_CONFIG</span>[<span class="string">"weiXin"</span>] = array(</div><div class="line"></div><div class="line">        <span class="string">'account'</span> =&gt; <span class="string">'微信公众平台登录帐号'</span>,<span class="comment">#填写你注册的微信订阅号的帐号和密码</span></div><div class="line"></div><div class="line">        <span class="string">'password'</span> =&gt; <span class="string">'微信公众平台登录密码'</span>,</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改test.php文件,只保留如下几行即可：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">require <span class="string">"config.php"</span>;</div><div class="line">require <span class="string">"include/WeiXin.php"</span>;</div><div class="line"></div><div class="line"><span class="variable">$weiXin</span> = new WeiXin(<span class="variable">$G_CONFIG</span>[<span class="string">'weiXin'</span>]);</div><div class="line"></div><div class="line"><span class="variable">$testFakeId</span> = <span class="string">"<span class="variable">$argv</span>[1]"</span>;<span class="comment">#微信好友ID号，这里通过nagios传入</span></div><div class="line"></div><div class="line"><span class="variable">$msg</span> = `cat /usr/<span class="built_in">local</span>/nagios/var/nagios.msg`;<span class="comment">#要发送的报警信息，由nagios传入</span></div><div class="line"></div><div class="line">print_r(<span class="variable">$weiXin</span>-&gt;send(<span class="variable">$testFakeId</span>, <span class="string">"<span class="variable">$msg</span>"</span>));<span class="comment">#给微信好友发送信息</span></div></pre></td></tr></table></figure>
<h2 id="3-整合nagios和微信公共平台私有接口"><a href="#3-整合nagios和微信公共平台私有接口" class="headerlink" title="3.整合nagios和微信公共平台私有接口"></a>3.整合nagios和微信公共平台私有接口</h2><h3 id="增加微信报警选项-templates-cfg"><a href="#增加微信报警选项-templates-cfg" class="headerlink" title="增加微信报警选项: templates.cfg"></a>增加微信报警选项: templates.cfg</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 /usr/local/nagios/etc/objects/templates.cfg</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 define contact{…} 部分,将以下两行:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">host_notification_commands notify-host-by-email</div><div class="line">service_notification_commands notify-service-by-email</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">host_notification_commands notify-host-by-email,notify-host-by-weixin</div><div class="line">service_notification_commands notify-service-by-email,notify-service-by-weixin</div></pre></td></tr></table></figure>
<h3 id="增加调用命令-commands-cfg"><a href="#增加调用命令-commands-cfg" class="headerlink" title="增加调用命令: commands.cfg"></a>增加调用命令: commands.cfg</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 /usr/local/nagios/etc/objects/commands.cfg</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在该文件的最后增加以下部分:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">        <span class="comment">####notify-service-by-weixin</span></div><div class="line">define <span class="built_in">command</span>&#123;</div><div class="line">                command_name notify-service-by-weixin</div><div class="line">                command_line /usr/bin/<span class="built_in">printf</span> <span class="string">"%b"</span> <span class="string">"***** Nagios *****\n\nNotification Type: <span class="variable">$NOTIFICATIONTYPE</span>$\nHost: <span class="variable">$HOSTNAME</span>$\nState: <span class="variable">$HOSTSTATE</span>$\nAddress: <span class="variable">$HOSTADDRESS</span>$\nInfo: <span class="variable">$HOSTOUTPUT</span>$\n\nDate/Time: <span class="variable">$LONGDATETIME</span>$\n"</span> &gt; /usr/<span class="built_in">local</span>/nagios/var/nagios.msg &amp;&amp; /usr/bin/php /usr/<span class="built_in">local</span>/nagios/libexec/weixin/test.php <span class="variable">$CONTACTADDRESS1</span>$  &amp;&gt;/dev/null        </div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="comment">####notify-host-by-weixin        </span></div><div class="line">define <span class="built_in">command</span>&#123;</div><div class="line">                command_name notify-host-by-weixin</div><div class="line">                command_line /usr/bin/<span class="built_in">printf</span> <span class="string">"%b"</span> <span class="string">"***** Nagios *****\n\nNotification Type: <span class="variable">$NOTIFICATIONTYPE</span>$\nHost: <span class="variable">$HOSTNAME</span>$\nState: <span class="variable">$HOSTSTATE</span>$\nAddress: <span class="variable">$HOSTADDRESS</span>$\nInfo: <span class="variable">$HOSTOUTPUT</span>$\n\nDate/Time: <span class="variable">$LONGDATETIME</span>$\n"</span> &gt; /usr/<span class="built_in">local</span>/nagios/var/nagios.msg &amp;&amp; /usr/bin/php /usr/<span class="built_in">local</span>/nagios/libexec/weixin/test.php <span class="variable">$CONTACTADDRESS1</span>$  &amp;&gt;/dev/null        </div><div class="line">        &#125;</div></pre></td></tr></table></figure>
<h3 id="修改联系人选项-contact-cfg"><a href="#修改联系人选项-contact-cfg" class="headerlink" title="修改联系人选项: contact.cfg"></a>修改联系人选项: contact.cfg</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 /usr/local/nagios/etc/objects/contact.cfg</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 define contact{…} 部分增加如下一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">address     11206***<span class="comment">#微信好友ID，登录微信公众平台网页版，在用户管理中点击你要发微信的好友，此时在地址上显示的fakeid就是微信好友的ID。</span></div></pre></td></tr></table></figure>
<h3 id="重载nagios配置"><a href="#重载nagios配置" class="headerlink" title="重载nagios配置"></a>重载nagios配置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service nagios reload</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nagios%E6%95%B4%E5%90%88%E5%BE%AE%E4%BF%A1%E8%AE%A2%E9%98%85%E5%8F%B7%E6%8A%A5%E8%AD%A6/01.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nagios%E6%95%B4%E5%90%88%E5%BE%AE%E4%BF%A1%E8%AE%A2%E9%98%85%E5%8F%B7%E6%8A%A5%E8%AD%A6/02.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nagios/3. nagios配置邮件告警/">nagios配置邮件告警</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目前 nagios 只能在浏览器上查看各个服务的状态，当某个机器宕掉或者某个服务宕掉时，我们是不知道的，因为我们不可能一直盯着服务器看。这时候，就需要用到警告系统了，让它自动化，当发现问题时及时通知到我们。下面配置使用发邮件的方式来实现告警。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下操作都在服务器上完成。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先定义发邮件接收者。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># vim /etc/nagios/objects/contacts.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">define contact&#123;</div><div class="line">    contact_name 123</div><div class="line">    use generic-contact</div><div class="line">    <span class="built_in">alias</span> yanyi</div><div class="line">    email 18983111118@189.cn</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">define contact&#123;</div><div class="line">    contact_name 456</div><div class="line">    use generic-contact</div><div class="line">    <span class="built_in">alias</span> aaa</div><div class="line">    email 89429541@qq.com</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">define contactgroup&#123;</div><div class="line">    contactgroup_name common</div><div class="line">    <span class="built_in">alias</span> common</div><div class="line">    members 123,456</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： contacts.cfg 里面既可以定义 user 也可以定义 group ，先定义两个 user 123 和 456 ，然后把这两个 user 加入到 common 组里面。等会发邮件就发给 common 组就可以了，那这样 18983111118@189.cn 和 89429541@qq.com 都会收到邮件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在需要告警的服务里面加上 contactgroup</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># vim /etc/nagios/conf.d/192.168.0.98.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;针对 check_load 服务增加告警相关的配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_load</div><div class="line">    check_command check_nrpe!check_load</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">    contact_groups common</div><div class="line">    notifications_enabled 1</div><div class="line">    notification_period 24x7</div><div class="line">    notification_options w,u,c,r</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：notifications_enabled  1  表示是否开启提醒功能。 1 为开启，0 为禁用。一般，这个选项会在主配置文件 （nagios.cfg） 中定义，效果相同。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;notification_period 24×7 表示发送提醒的时间段。非常重要的主机（服务）定义为问题发生，都不会发送提醒。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;notification_options:w,u,c,r 表示 service 的状态。w 为 waning，u 为 unknown，c 为 critical，r 为 recover（恢复了），类似的还有一个 host对应的状态：d,u,r    d 状态为 DOWN，u 状态为 UNREACHABLE，r 状态恢复为 OK，需要加入到 host 的定义配置里。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑完成配置文件后，需要重启 nagios 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># service nagios restart</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nagios/2. nagios 监控客户端/">nagios 监控客户端</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在客户端机器阿航安装 epel 扩展源</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y epel-release</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装 nagios 以及 nagios-plugins</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/nagios/nrpe.cfg</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到 “allowed_hosts=127.0.0.1” 改为 “allowed_hosts=127.0.0.1，192.168.0.62 ” 后面的 ip 为服务端 ip ； 找到 “dont_blame_nrpe=0” 改为 “dont_blam_nrpe=1” </p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/02.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动客户端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/nrpe start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端配置好以后，在到服务端继续配置。客户端 ip 为 192.168.0.98，下面定义子配置文件。（以下操作在服务端上完成）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># cd /etc/nagios/conf.d/</span></div><div class="line">[root@nagios conf.d]<span class="comment"># vim /192.168.0.98.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">define host&#123;</div><div class="line">    use linux-server</div><div class="line">    host_name 192.168.0.98</div><div class="line">    <span class="built_in">alias</span> 0.98</div><div class="line">    address 192.168.0.98</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_ping</div><div class="line">    check_command check_ping!100.0,20%!200.0,50%</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_ssh</div><div class="line">    check_command check_ssh</div><div class="line">    max_check_attempts 5 </div><div class="line">    normal_check_interval 1 </div><div class="line">    notification_interval 60</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_http</div><div class="line">    check_command check_http</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：</p>
<ul>
<li>“max_check_attempts  5” ：表示当nagios检测到问题时，一共尝试检测5次都有问题才会告警，如果该数值为1，那么检测到问题立即告警</li>
<li>“normal_check_interval 1”：表示重新检测的时间间隔，单位是分钟，默认是3分钟</li>
<li>“notification_interval 60”：表示在服务出现异常后，故障一直没有解决，nagios再次对使用者发出通知的时间。单位是分钟。如果你认为，所有的事件只需要一次通知就够了，可以把这里的选项设为0。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上服务不依赖于客户端 nrpe 服务，可以想象，在子机电脑上可以使用 ping 或者 telnet 探测远程任何一台机器是否存活、是否开启某个端口或服务。而当想要检测客户端上的某个具体服务的情况时，就需要借助于 nrpe 了，比如想知道客户端机器的负载或磁盘使用情况。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑完成配置文件后，检测配置是否有错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># nagios -v /etc/nagios/nagios.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再服务端重启一下 nagios 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># service nagios restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在浏览器里访问 nagios ，刷新会发现多出来一台主机，并且多出来三个服务。只不过这三个服务并不是我们想要的，想要监控系统负载，监控磁盘使用率等服务，这时候就要使用 nrpe 服务了。继续在服务端上添加服务。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># vim /etc/nagios/objects/commands.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">define <span class="built_in">command</span>&#123;</div><div class="line">    command_name check_nrpe</div><div class="line">    command_line <span class="variable">$USER1</span>$/check_nrpe -H <span class="variable">$HOSTADDRESS</span>$ -c <span class="variable">$ARG1</span>$</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后继续编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># vim 192.168.0.98.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_load</div><div class="line">    check_command check_nrpe!check_load</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_disk_sda1</div><div class="line">    check_command check_nrpe!check_sda1</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_disk_sda3</div><div class="line">    check_command check_nrpe!check_sda3</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：“check_nrpe!check_load” 这里的 check_nrpe 就是在 commands.cfg 刚刚定义的，check_load 是远程主机上的一个检测脚本。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在远程主机上编辑 nrpe.cfg 配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># vim /etc/nagios/nrpe.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;搜索 check_load ，这行就是在服务器上要执行的脚本了。然后把 check_hda1 更改一下：/dev/hds1 改为 ，/dev/sda1 。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再加一行</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端上重启一下 nrpe 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># service nrpe restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端也重启下 nagios 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># service nagios restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再到浏览器刷新，会看到又多出来三个服务</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/08.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;稍微等一会就可以查看到具体的状态了。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/09.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Nagios/1. Nagios 安装/">Nagios安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://nagios.org" target="_blank" rel="external">Nagios官网</a></p>
<h2 id="1-nagios-简介"><a href="#1-nagios-简介" class="headerlink" title="1.nagios 简介"></a>1.nagios 简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nagios 是一个开源软件，可以监控网络设备网络流量、linux/windows 主机状态，甚至可以监控打印机。它可以运行在 linux 上或 windows 上。基于浏览器的 web 界面方便运维人员查看监控项目的状态，支持 web 界面配置、管理操作、支持短信、邮件通知，可以自定义脚本实现自定义化监控。</p>
<h2 id="2-nagios-安装-服务端（192-168-0-62）"><a href="#2-nagios-安装-服务端（192-168-0-62）" class="headerlink" title="2.nagios 安装 - 服务端（192.168.0.62）"></a>2.nagios 安装 - 服务端（192.168.0.62）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先来安装服务器，nagios 同样也是需要 apache + php 的环境，centos 6 默认的 yum 源里没有 nagios 相关的 rpm 包，需要安装一个 epel 的扩展源。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># yum install -y epel-release</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后安装 nagios 相关的包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置登录 nagios 后台的用户和密码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># htpasswd -c /etc/nagios/passwd nagiosadmin</span></div><div class="line">New password: </div><div class="line">Re-type new password: </div><div class="line">Adding password <span class="keyword">for</span> user nagiosadmin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查配置文件是否有问题</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># nagios -v /etc/nagios/nagios.cfg</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E5%AE%89%E8%A3%85/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明配置文件没有问题</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># service httpd start</span></div><div class="line">[root@nagios ~]<span class="comment"># service nagios start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;浏览器访问 <a href="http://ip/nagios" target="_blank" rel="external">http://ip/nagios</a></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E5%AE%89%E8%A3%85/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入设置的帐号密码登录</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E5%AE%89%E8%A3%85/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时，nagios 监控的只有 localhost ，还没有其他机器，要想添加监控客户机，还需要在客户端安装 nagios 相关的软件包，并且需要在服务端配置。</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/27">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
